<template>
  <div class="content-wrapper">
    <div class="nav">
      <el-breadcrumb>
        <el-breadcrumb-item :to="{ path: '/administration-employeeList' }">人事档案</el-breadcrumb-item>
        <el-breadcrumb-item>通讯录列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- <span><el-button type="primary" @click="syncDingUser">刷新通讯录</el-button></span> -->
    </div>
    <div class="content">
      <div class="search-bar">
        <el-input placeholder="按照名字模糊查询" prefix-icon="el-icon-search" v-model="filterText" style="width: 180px;" clearable></el-input>
      </div>
      <div class="main-content">
        <el-tree class="filter-tree" node-key="id" :data="deptList" :props="{children: 'children', label: 'name'}" :filter-node-method="filterNode" :expand-on-click-node="false" :default-expanded-keys="['1']" ref="tree">
          <div class="custom-tree-node" slot-scope="{ data }">
            <div class="dept" v-if="data.id">{{ data.name }}</div>
            <div v-if="!data.id">
              <div v-for="employee in data" :key="employee.dingId">
                <div v-if="employee.realName.indexOf(filterText) !== -1" class="employee" @click="showDetailDialog(employee.dingId, false)">
                  <span><el-avatar :size="24" :src="employee.avatar"></el-avatar>&nbsp;&nbsp;{{ employee.realName }}</span>
                  <span><i class="el-icon-search" v-if="isAuth('archives:person:info')"></i></span>
                </div>
              </div>
            </div>
          </div>
        </el-tree>
      </div>
    </div>
    <personalDetail ref="personalDetail" v-if="dialogVisible"></personalDetail>
  </div>
</template>

<script>
import dingMixin from  '@/mixins/ding'
import personalDetail from './personal-detail'
export default {
  data () {
    return {
      filterText: '',
      deptList: [],
      employeeList: [],
      dialogVisible: false,
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  components: {
    personalDetail
  },
  mixins: [dingMixin],
  mounted () {
    this.getAllDeptList();
    //通知跳转
    if(this.$route.params.id) this.showDetailDialog(this.$route.params.id, true)
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true;
      if(!data.id) {
        let employee = data.filter(employee => { return employee.realName.indexOf(value) !== -1})
        return employee.length > 0;
      }
    },
    async getAllDeptList() {
      let list = await this.$httpN.postB('sys/dept/listAll');
      let {data} = await this.$http({
        url: this.$http.adornUrl('hr/user/list'),
        method: 'post',
        data: this.$http.adornData({
          page: 1,
          limit: 999,
          status: '1'
        })
      })
      let employeeList = [];
      if (data && data.code === 0) employeeList = data.page.list
      list.forEach( (item, index) => {
        item.employee = []
        employeeList.forEach( employee => {
          if(employee.departmentIds !== '' && employee.departmentIds.split(',').includes(item.id) ) item.employee.push(employee)
        })
      })
      this.deptList = this.treeData(list)
    },
    treeData(list) {
      return list.filter(father=>{
        let branchArr = list.filter(child=>father.id === child.pid)
        branchArr.length>0 ? father.children = branchArr : ''
        if(father.employee.length > 0) {
          if( !father.children ) father.children = []
          father.children.push(father.employee)
        }
        return father.pid === '0';
      })
    },
    showDetailDialog(dingId, ifToDo) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.personalDetail.init(dingId, true, ifToDo);
      })
    },
    async syncDingUser() {
      if ( !await this.$httpN.postB('ding/syncDingUser') ) return
    }
  }
}
</script>

<style lang='scss' scoped>
.content-wrapper {
  height: 100vh;
  min-width: 500px;
  .nav {
    display: flex;
    height: 50px;
    line-height: 50px;
    padding: 0 20px;
    justify-content: space-between;
    border-bottom: 1px solid #DCDFE6;
    .el-breadcrumb {
      display: flex;
      align-items: center;
      .el-breadcrumb__item {
        float: none;
      }
    }
  }
  .content {
    height: calc(100vh - 50px);
    overflow: auto;
    .search-bar {
      padding: 0 20px;
      height: 50px;
      display: flex;
      align-items: center;
    }
    .main-content {
      margin-bottom: 50px;
      padding: 0 20px;
    }
  }
}
.filter-tree {
  max-width: 800px;
  /deep/ .el-tree-node__content {
    height: auto;
    align-items: start;
    background-color: #FFF;
    cursor: default;
    .el-tree-node__expand-icon {
      padding: 12px;
    }
  }
  .custom-tree-node {
    line-height: 36px;
    width: 100%;
    .employee {
      display: flex;
      justify-content: space-between;
      cursor: pointer;
      padding-right: 20px;
      &:hover {
        background-color: #F6F6F6;
        color: #409EFF;
      }
      span {
        display: flex;
        align-items: center;
      }
    }
  }
}
</style>